{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "092ef445",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "\u001b[31m   WARNING: This is a development server. Do not use it in a production deployment.\u001b[0m\n",
      "\u001b[2m   Use a production WSGI server instead.\u001b[0m\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:30] \"GET / HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:30] \"GET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:30] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:32] \"GET /index_vowels HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:32] \"GET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:32] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:34] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:35] \"GET /index HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:35] \"GET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:35] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:38] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:44] \"GET /index_vowels HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:44] \"GET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:44] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:46:48] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:11] \"GET /index HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:12] \"GET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:12] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:15] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:18] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:32] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:33] \"GET /index_vowels HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:33] \"GET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:33] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:35] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:36] \"GET /index HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:36] \"GET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:36] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 16:53:37] \"GET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\" 404 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask,request\n",
    "from flask import render_template\n",
    "\n",
    "# flask角色： 中央登记中心\n",
    "# 1. 路由 route：\n",
    "# 1.1  url 相对路径：不完整的路径（相对于IP地址的路径，所以IP地址和端口号不需要写）；\n",
    "#    绝对路径是完整路径。\n",
    "# 1.2  request 的请求方法：【GET默认】【POST】\n",
    "# 2  视图函数\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "# 【GET默认】\n",
    "@app.route(\"/\")\n",
    "def hello_world():\n",
    "    return render_template('hello.html')\n",
    "\n",
    "# 【GET默认】\n",
    "@app.route(\"/index\")\n",
    "def index():\n",
    "    return render_template('index.html')\n",
    "\n",
    "# 【GET默认】\n",
    "@app.route(\"/index_vowels\")\n",
    "def index_vowels():\n",
    "    return render_template('index_vowels.html')\n",
    "\n",
    "# 【POST】:用户提交的数据用POST方法---名字全称\n",
    "@app.route(\"/result\",methods=['POST'])\n",
    "def result():\n",
    "    # 我们怎么接受数据？ flask提供了request.form的方法用来\n",
    "    # 接收用户数据\n",
    "    # 第一个项目：名字的全称\n",
    "    py_firstname = request.form['firstname'] \n",
    "    py_lastname = request.form['lastname']\n",
    "    py_fullname = py_firstname.title() + ' ' + py_lastname.title()\n",
    "            \n",
    "    return render_template('result.html',\n",
    "                          fullname = py_fullname\n",
    "                          )\n",
    "\n",
    "# 【POST】:用户提交的数据用POST方法---元音字母\n",
    "@app.route(\"/result_vowels\",methods=['POST'])\n",
    "def result_vowels():\n",
    "    # 我们怎么接受数据？ flask提供了request.form的方法用来\n",
    "    # 接收用户数据\n",
    "    \n",
    "    # 第二个项目：元音字母表词频统计\n",
    "    py_word = request.form['word']\n",
    "    py_vowels = request.form['vowels']\n",
    "    py_found = {}\n",
    "    for i in py_word:\n",
    "        if i in py_vowels:\n",
    "            py_found.setdefault(i,0)\n",
    "            py_found[i]+=1\n",
    "            \n",
    "    return render_template('result_vowels.html',\n",
    "                          found = py_found)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73a9bbbd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
